package com.geping.etl.utils;

import org.apache.commons.lang.StringUtils;

/**
 *  HTML 工具类
 * @author  liang.xu
 * @date 2021年5月8日
 */
public class HtmlUtils {

    /**
     * script 开始标签
     */
    private static String script_start="<script>";

    /**
     * script 结束标签
     */
    private static String script_end="</script>";

    /**
     * 转义特殊字符
     * @param source
     * @return
     */
    public static String htmlEncode(String source) {
        if (StringUtils.isBlank(source)) {
            return "";
        }
        String html = "";
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < source.length(); i++) {
            char c = source.charAt(i);
            switch (c) {
                case '<':
                    buffer.append("&lt;");
                    break;
                case '>':
                    buffer.append("&gt;");
                    break;
                case '&':
                    buffer.append("&amp;");
                    break;
              default:
                buffer.append(c);
             }
        }
        html = buffer.toString();
        return html;
    }

    /**
     * 过滤
     * @param source
     * @return
     */
    public static String htmlFilter(String source) {
        if (StringUtils.isBlank(source)) {
            return "";
        }
      return scriptFilter(source);
    }

    private static String scriptFilter(String source) {
       return  source.replace(script_start,"").replace(script_end,"");
    }

    public static void main(String[] args) {
        String v="0517xlXL&";
        System.out.println(HtmlUtils.htmlEncode(v));
    }
}